local jianjie = fk.CreateSkill {
  name = "tyj_heg__jianjie",
  tags = { Skill.Compulsory },
}
local H = require "packages.ol_hegemony.util"
local U = require "packages.utility.utility"
local mybiangeng = function(player, isMain, num, isHidden, extraGen)
  local room = player.room ---@class Room
  player, isMain, isHidden = player, isMain or false, isHidden or false
  num = num or 3
  local data = {
    isMain = isMain,
    isHidden = isHidden,
    num = num,
  }

  if room.logic:trigger(H.TransformGeneraling, player, data) then return false end
  isMain = data.isMain
  isHidden = data.isHidden
  num = data.num
  local orig = isMain and player.general or player.deputyGeneral
  num = num or 3
  if not orig then return false end
  if orig == "anjiang" then
    player:revealGeneral(not isMain, true)
    orig = isMain and player.general or player.deputyGeneral
  end
  local existingGenerals = {}
  for _, p in ipairs(room.players) do
    table.insert(existingGenerals, H.getActualGeneral(p, false))
    table.insert(existingGenerals, H.getActualGeneral(p, true))
  end
  local kingdom = player:getMark("__heg_kingdom")
  if kingdom == "wild" then
    kingdom = player:getMark("__heg_init_kingdom")
  end
  extraGen = extraGen or {}
  local generals = room:findGenerals(function(g)
    return Fk.generals[g].kingdom == kingdom or Fk.generals[g].subkingdom == kingdom or table.contains(extraGen, g)
  end, num)
  local general = room:askToChooseGeneral(player, { generals = generals, n = 1, no_convert = true }) ---@type string
  table.removeOne(generals, general)
  table.insert(generals, orig)
  room:returnToGeneralPile(generals, "bottom")   --塞回武将牌堆底
  room:setEmotion(player, "./packages/ol_hegemony/image/anim/transformation")
  room:changeHero(player, general, false, not isMain, not isHidden, false, false)
  --暗置变更
  if isHidden then
    player:hideGeneral(not isMain)
    room:sendLog {
      type = "#ChangeHiddenGeneral",
      from = player.id,
      toast = true,
    }
  end
  room:setPlayerMark(player, isMain and "__heg_general" or "__heg_deputy", general)
  H.judegHegMark(player)
  room.logic:trigger(H.TransformGeneraled, player, orig)
end
Fk:loadTranslationTable {
  ["tyj_heg__jianjie"] = "荐杰",
  [":tyj_heg__jianjie"] = "锁定技，你明置此武将牌后，变更此武将牌并重新结算珠联璧合标记，若你的另一张武将牌已明置，则优先观看能与其组成珠联璧合关系的武将牌。",
  ["$tyj_heg__jianjie1"] = "使君匡扶天下之愿，此二人可助之。",
  ["$tyj_heg__jianjie2"] = "使君如欲匡扶汉室，我可荐一人。",
}
jianjie:addEffect(fk.GeneralRevealed, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player == target and player:hasSkill(jianjie.name) then
      for _, v in pairs(data) do
        if table.contains(Fk.generals[v]:getSkillNameList(), jianjie.name) then return true end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local isMain = player.general == "tyj_heg__simahui" and true or false
    local generalDeck = room.general_pile
    local other = "anjiang"
    if isMain then
      other = player.deputyGeneral
    else
      other = player.general
    end
    local topGenerals = {}
    if other ~= "" and other ~= "anjiang" then
      local tos = table.filter(room.general_pile, function(thisgen)
        return Fk.generals[thisgen]:isCompanionWith(Fk.generals[other])
      end
      )
      if #tos > 0 then
        local targetGenerals = table.random(tos, math.min(#tos, 3))
        local otherGenerals = {}

        for _, general in ipairs(generalDeck) do
          if table.contains(targetGenerals, general) then
            table.insert(topGenerals, general)
          else
            table.insert(otherGenerals, general)
          end
        end

        local newDeck = {}
        for _, general in ipairs(topGenerals) do
          table.insert(newDeck, general)
        end
        for _, general in ipairs(otherGenerals) do
          table.insert(newDeck, general)
        end
        room.general_pile = newDeck
      end
    end
    mybiangeng(player, isMain, 3, false, topGenerals)
    --H.transformGeneral(player, isMain, 3, false)
    if Fk.generals[player.general]:isCompanionWith(Fk.generals[player.deputyGeneral]) and player:getMark("@!!companion") == 0 then
      H.addHegMark(room, player, "companion")
    end
  end,
})
return jianjie
